<?php
class BasePayment{

    var $_gateway   = '';
    /* 支付方式唯一标识 */
    var $_code      = '';
    var $_pid		= '';
	/* 构造函数 */
    function __construct($payment_info = array()){
        $this->_info   = $payment_info;
        $this->_pid		= $payment_info['payment_id'];
        $this->_config = unserialize($payment_info['config']);
    }
    /* 获取支付表单 */
    function get_payform(){
    	return $this->_create_payform('POST');
    }
    /* 获取规范的支付表单数据 */
    function _create_payform($method = '', $params = array()){
        return array(
            'online'    =>  $this->_info['is_online'],
            'desc'      =>  $this->_info['payment_desc'],
            'method'    =>  $method,
            'gateway'   =>  $this->_gateway,
            'params'    =>  $params,
        );
    }
    /* 获取通知地址 */
    function _create_notify_url($order_sn_main,$type){
        if(!$order_sn_main || !$type){
            $this->_error('miss order_sn_main or payment_type');
            exit;
        }
        return url("index.php?c=paycallback&a={$type}&main={$order_sn_main}");
    }
    /* 获取返回地址 */
    function _create_return_url($order_sn_main,$type){
        if(!$order_sn_main || !$type){
            $this->_error('miss order_sn_main or payment_type');
            exit;
        }
        return url("index.php?c=paycallback&a={$type}&main={$order_sn_main}");
    }
    /* 获取外部交易号 */
    protected function _get_trade_sn($order_sn_main,$money){
    	$out_order_sn = $this->_make_trade_sn();
    	$is_exist = modcall("paysign","get",array("out_order_sn={$out_order_sn}"));
        if(!empty($is_exist)){//重复了
            return $this->_get_trade_sn($order_sn_main,$money);
        }
    	
    	$data = array(
    		'order_sn_main'=>$order_sn_main,
    		'out_order_sn'=>$out_order_sn,
    		'pay_id' => $this->_pid,
    		'money'	=> $money,
    		'ctime'	=> gmtime(),
    		'status'=> 'ready'
    	);

        $id=modcall("paysign","add",array($data));
    	if($id){
    		return $out_order_sn;
    	}
    }
    /* 生成外部交易号 */
    protected function _make_trade_sn(){
        /* 选择一个随机的方案 */
        $timestamp = gmtime();
        $ym = date('ym', $timestamp);
        $out_trade_sn = $ym . str_pad(mt_rand(1,999999),6, '0', STR_PAD_LEFT);
        return $out_trade_sn;
    }
    /* 获取商品简介 */
    function _get_subject($order_sn_main){
        return '中国·品牌商城订单 : ' . $order_sn_main;
    }
    /* 将验证结果反馈给网关 */
    function verify_result($result){
        if ($result){
            echo 'success';
        }else{
            echo 'fail';
        }
    }
}
?>